import { PotView } from './PotView';
import * as THREE from 'three';

export class LineView {
    id: string;
    mesh: THREE.ArrowHelper;
    from: PotView;
    to: PotView;
    origin: any;

    static headLength = 6;
    static headWidth = 4;

    constructor(id: string, from: PotView, to: PotView, color: number, origin: any) {
        this.id = id;
        this.from = from;
        this.to = to;
        this.origin = origin;
        this.mesh = new THREE.ArrowHelper(new THREE.Vector3(1, 0, 0), new THREE.Vector3(), length, color);
    }

    update() {
        let t1 = this.from.body.position;
        let t2 = this.to.body.position;
        let p1 = new THREE.Vector3(t1.x, t1.y, 0);
        let p2 = new THREE.Vector3(t2.x, t2.y, 0);

        let len = p1.distanceTo(p2) - 4;
        if(len < 0.1){
            len = 0.1;
        }
        this.mesh.setLength(len, LineView.headLength, LineView.headWidth)
        this.mesh.setDirection(new THREE.Vector3().subVectors(p2, p1).normalize());
        this.mesh.position.copy(p1);
        this.mesh.updateMatrixWorld();
    }
}